package com.xique.park.bean.transform.web.garageLaneDev;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.ParkCarMngtype;
import com.xique.park.bean.domain.ParkGarage;
import com.xique.park.bean.request.web.garageLaneDev.garage.AddGarageRequest;
import com.xique.park.bean.request.web.garageLaneDev.garage.EditGarageRequest;
import com.xique.park.bean.vo.web.garageLaneDeviceTree.TreeNodeGarageMngTypeVo;
import com.xique.park.bean.vo.web.garageLaneDeviceTree.TreeNodeGarageVo;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class GarageTransform {

    /**
     * 车库转化为VO实体
     */
    public static TreeNodeGarageVo transformNodeGarageVo(ParkGarage parkGarage, List<ParkCarMngtype> mngTypeList) {
        TreeNodeGarageVo treeGarageVo = new TreeNodeGarageVo();
        treeGarageVo.setId(parkGarage.getId());
        treeGarageVo.setName(parkGarage.getName());
        treeGarageVo.setType("garage");
        treeGarageVo.setGarageType(parkGarage.getType());
        treeGarageVo.setTotalSpace(parkGarage.getTotalSpace());
        treeGarageVo.setStatus(parkGarage.getStatus());
        treeGarageVo.setModifySpace(parkGarage.getTempEmptySpace());
        treeGarageVo.setCreateTime(parkGarage.getCreateTime());
        treeGarageVo.setUpdateTime(parkGarage.getUpdateTime());
        String[] mngTypeIds = StringUtils.isNotEmpty(parkGarage.getCountSpaceMngtype()) ? parkGarage.getCountSpaceMngtype().split(",") : new String[]{"0"};
        treeGarageVo.setMngTypeList(transformGarageMngTypeList(mngTypeList, mngTypeIds));
        return treeGarageVo;
    }

    /**
     * 根据查询出的车辆管理类型，转化为前端实体(添加车库时使用，是否选中，统一选N)
     */
    public static List<TreeNodeGarageMngTypeVo> transformGarageMngTypeList(List<ParkCarMngtype> list, String[] selectedMngTypeIds) {
        if (CollectionUtils.isNotEmpty(list)) {
            ParkCarMngtype mngTypeAll = new ParkCarMngtype();
            mngTypeAll.setId(0L);
            mngTypeAll.setName("全部类型");
            list.add(mngTypeAll);
            return list.stream().map(result -> {
                TreeNodeGarageMngTypeVo vo = new TreeNodeGarageMngTypeVo();
                vo.setId(result.getId());
                vo.setName(result.getName());
                vo.setSelected(Arrays.asList(selectedMngTypeIds).contains(result.getId().toString()) ? "Y" : "N");
                return vo;
            }).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }

    }

    /**
     * 添加车库request转化为garage实体
     */
    public static ParkGarage transformAddGarageRequest(AddGarageRequest request) {
        ParkGarage parkGarage = new ParkGarage();
        parkGarage.setParkId(SecurityUtils.getParkId());
        parkGarage.setCountSpaceMngtype(request.getSelectedMngTypeIds());
        parkGarage.setPid(null != request.getParentGarageId() ? request.getParentGarageId() : 0L);
        parkGarage.setName(request.getName());
        parkGarage.setStatus(request.getStatus());
        parkGarage.setTempEmptySpace(request.getModifySpace());
        parkGarage.setTotalSpace(request.getTotalSpace());
        parkGarage.setType(request.getGarageType());
        //TODO 空车位数暂时放所有车位
        parkGarage.setEmptySpace(request.getTotalSpace());
        return parkGarage;
    }

    /**
     * 编辑车库request转化为garage实体
     */
    public static ParkGarage transformEditGarageRequest(EditGarageRequest request) {
        ParkGarage parkGarage = new ParkGarage();
        parkGarage.setId(request.getId());
        parkGarage.setParkId(SecurityUtils.getParkId());
        parkGarage.setCountSpaceMngtype(request.getSelectedMngTypeIds());
        parkGarage.setPid(null != request.getParentGarageId() ? request.getParentGarageId() : 0L);
        parkGarage.setName(request.getName());
        parkGarage.setStatus(request.getStatus());
        parkGarage.setTempEmptySpace(request.getModifySpace());
        parkGarage.setTotalSpace(request.getTotalSpace());
        parkGarage.setType(request.getGarageType());
        //TODO 空车位数暂时放所有车位
        parkGarage.setEmptySpace(request.getTotalSpace());
        return parkGarage;
    }
}
